﻿using System;
using System.Collections.Generic;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web;
using System.Drawing;

namespace Shamrock.Helpers
{
    public class ImageProcessor
    {
        private static readonly Dictionary<string, string> _imageTypes = new Dictionary<string, string>()
                                                                             {
                                                                                 {"image/gif", ".gif"},
                                                                                 {"image/jpeg", ".jpg"},
                                                                                 {"image/pjpeg", ".jpg"},
                                                                                 {"image/png", ".png"},
                                                                                 {"image/x-png", ".png"}
                                                                             };
        public int Width
        {
            get; private set;
        }
        public int Height
        {
            get; private set;
        }
        public int Size
        {
            get; private set;
        }

        public static bool ValidateContentType(string contentType)
        {
            return _imageTypes.ContainsKey(contentType.ToLower());
        }
        public static string ContentTypeToFileExtension(string contentType)
        {
            return _imageTypes[contentType.ToLower()];
        }

        public static string CurrentTimestamp()
        {
            return (DateTime.UtcNow.Ticks - 621355968000000000).ToString().Substring(0, 13);
        }
        
        public static string CreateFileName(HttpPostedFileBase hpf)
        {
            return (CurrentTimestamp() + ContentTypeToFileExtension(hpf.ContentType));
        }

        public static Dimension SaveThumbnail(Bitmap bitmap, string filePath, int maxWidth, int maxHeight)
        {
            try
            {
                if (bitmap.Width < maxWidth && bitmap.Height < maxHeight)
                {
                    bitmap.Save(filePath, ImageFormat.Jpeg);
                    return new Dimension(bitmap.Width, bitmap.Height);
                }
                else
                {
                    int thumbWidth;
                    int thumbHeight;
                    if (bitmap.Width > bitmap.Height)
                    {
                        double resizeRatio = maxWidth / (double)bitmap.Width;
                        thumbWidth = maxWidth;
                        thumbHeight = (int)(bitmap.Height * resizeRatio);
                    }
                    else
                    {
                        double resizeRatio = maxHeight / (double)bitmap.Height;
                        thumbWidth = maxHeight;
                        thumbHeight = (int)(bitmap.Width * resizeRatio);
                    }

                    using (Bitmap thumbnail = new Bitmap(bitmap, thumbWidth, thumbHeight))
                    {
                        thumbnail.Save(filePath, ImageFormat.Jpeg);
                    }
                    return new Dimension(thumbWidth, thumbHeight);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        public static Dimension SaveThumbnail(Stream inputStream, string filePath, int maxWidth, int maxHeight)
        {
            try
            {
                using (Bitmap bitmap = (Bitmap)Image.FromStream(inputStream, true, true))
                {
                    return SaveThumbnail(bitmap, filePath, maxWidth, maxHeight);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}